package com.lam.framework.security.provider;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

import com.lam.common.base.domain.LoginUser;
import com.lam.common.context.TokenUtils;

@Component
public class LamAuthenticationProvider implements AuthenticationProvider {

//	@Autowired
//	private UserDetailsService userDetailsService;

	// 自定义密码验证
	@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		
//		String username = authentication.getName(); // 表单提交的用户名
//		String password = (String) authentication.getCredentials(); // 表单提交的密码
//		if (StringUtils.isBlank(password)) {
//			throw new BadCredentialsException("凭证为空");
//		} 
//		
//		LoginUser loginUser = (LoginUser) userDetailsService.loadUserByUsername(username);
//		if (!PasswordUtil.matches(username, password, loginUser.getSalt(), loginUser.getLoginPwd())) {
//			throw new BadCredentialsException("登录密码错误");
//		} 
		
		LoginUser loginUser = TokenUtils.getLoginUser();
		UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(loginUser,
				authentication.getCredentials(), loginUser.getAuthorities());
		result.setDetails(authentication.getDetails());
		return result;
	}

	@Override
	public boolean supports(Class<?> authentication) {
		return true;
	}

}